package com.tn.controller.alarm;

import com.gitee.hifong45.Ret;
import com.tn.controller.alarm.common.constants.Constants;
import com.tn.controller.alarm.common.enums.CommonCodeEnum;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.PageInput;
import com.tn.controller.alarm.common.model.PageResult;
import com.tn.controller.alarm.common.model.alarm.vo.AlarmQueryVO;
import com.tn.controller.alarm.common.model.alarm.vo.AlarmVO;
import com.tn.controller.alarm.common.model.vehicle.vo.AlarmInputParam;
import com.tn.exception.InvalidParamException;
import com.tn.service.alarm.IAlarmService;
import com.tn.utils.CheckParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 告警controller
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2022/2/17
 */
@Slf4j
@RestController
@RequestMapping("/aucy")
public class AlarmController {
    /**
     * service
     */
    @Resource
    private IAlarmService alarmService;


    @GetMapping("/addAlarm")
    public Ret<Object> addAlarm(AlarmInputParam alarm) {

        alarmService.saveAlarmInfo(alarm);
        return Ret.Ok();
    }

    /**
     * 更新告警参数
     *
     * @return 更新结果
     */
    @RequestMapping(value = "/queryAlarmParam", method = RequestMethod.GET)
    public CommonVO<Object> queryAlarmParam() {
        CommonVO<Object> result = new CommonVO<>();
        result.setData(alarmService.queryAlarmParam());
        return result;
    }

    /**
     * 更新告警参数
     *
     * @return 更新结果
     */
    @RequestMapping("/modifyAlarmParam")
    public CommonVO<Object> updateAlarmParam(@RequestBody Map<String, String> param) {
        CheckParamUtil.checkBlankRequiredParam(param.get(Constants.ALARM_PARAM));
        CommonVO<Object> result = new CommonVO<>();
        if (!alarmService.updateAlarmParam(Constants.ALARM_PARAM, param.get(Constants.ALARM_PARAM))) {
            result.setCode(CommonCodeEnum.UNKNOWN.getCode());
            result.setMsg(CommonCodeEnum.UNKNOWN.getMessage());
        }
        return result;
    }

    /**
     * 查询告警信息
     *
     * @param alarmPageQuery 分页查询条件
     * @return 查询结果
     * @throws InvalidParamException 参数异常
     */
    @RequestMapping("/queryAlarm")
    public CommonVO<PageResult<AlarmVO>> queryCamera(@RequestBody PageInput<AlarmQueryVO> alarmPageQuery)
            throws InvalidParamException {
        CheckParamUtil.checkPageParam(alarmPageQuery.getCurPage(), alarmPageQuery.getPageSize());
        CommonVO<PageResult<AlarmVO>> result = new CommonVO<>();
        result.setData(alarmService.queryAlarmInfo(alarmPageQuery));
        return result;
    }

    /**
     * 处理告警信息
     *
     * @param alarmQueryVO 处理情况入参
     * @return 处理结果
     */
    @RequestMapping("/handleAlarm")
    public CommonVO<Object> handleAlarm(@RequestBody AlarmQueryVO alarmQueryVO) {
        CheckParamUtil.checkNullRequiredParam(alarmQueryVO.getAlarmId());
        CheckParamUtil.checkBlankRequiredParam(alarmQueryVO.getAlarmHandler());
        CommonVO<Object> result = new CommonVO<>();
        if (!alarmService.handleAlarm(alarmQueryVO.getAlarmHandler(), alarmQueryVO.getAlarmId())) {
            result.setCode(CommonCodeEnum.UNKNOWN.getCode());
            result.setMsg(CommonCodeEnum.UNKNOWN.getMessage());
        }
        return result;
    }
}
